﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Server.Entity;
using Server.Utils;
using SocketServerHelper = Server.Utils.SocketServerHelper;

namespace Server
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        SocketServerHelper socketServerHelper;
        private void button1_Click(object sender, EventArgs e)
        {
            socketServerHelper = new SocketServerHelper(8081);
            socketServerHelper.ReciveAction = ReciveCallBack;
            socketServerHelper.LogAction = LogCallBack;
            socketServerHelper.SendProgressAction = SendProgreeCallBack;
            socketServerHelper.DisconnectAction = DisConnectionCallBack;
            socketServerHelper.Start();
        }

        private void SendProgreeCallBack(string ip,string arg1, int arg2, long arg3, bool arg4)
        {
            throw new NotImplementedException();
        }


        private void FrmMain_Load(object sender, EventArgs e)
        {


        }
        private void DisConnectionCallBack(string ip)
        {
            this.Invoke(new Action(() =>
            {
                foreach (DataGridViewRow item in dataGridView1.Rows)
                {
                    if (item.Cells[0].Value.ToString() == ip)
                    {
                        this.dataGridView1.Rows.Remove(item);
                    }
                }
            }));

        }
        private void LogCallBack(string ip ,string msg)
        {
            this.Invoke(new Action(() =>
            {
                this.richTextBox1.Text += msg + "\r\n";
            }));
        }
        private void ReciveCallBack(SendType sendType, string msg)
        {
            this.Invoke(new Action(() =>
            {
                if (sendType == SendType.对象)
                {
                    ClientMsgModel clientMsgModel = Newtonsoft.Json.JsonConvert.DeserializeObject<ClientMsgModel>(msg);
                    if (clientMsgModel == null) return;
                    bool isFind = false;

                    foreach (DataGridViewRow item in dataGridView1.Rows)
                    {
                        if (item.Cells[0].Value.ToString() == clientMsgModel.IP)
                        {
                            isFind = true;
                            item.Cells[1].Value = clientMsgModel.Status;
                        }
                    }

                    if (!isFind)
                    {
                        dataGridView1.Rows.Add(clientMsgModel.IP);
                    }
                }
            }));
        }
        private void SendProgreeCallBack(string fileName, int size, long length)
        {
            this.Invoke(new Action(() =>
            {
                this.label1.Text = $"当前传输文件:{fileName},{size}/{length}";
            }));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.button2.Enabled = false;
            var task = Task.Run(() =>
              {
                  if (socketServerHelper.IsConnected)
                  {
                      foreach (DataGridViewRow item in dataGridView1.Rows)
                      {
                          socketServerHelper.SendFolder(item.Cells[0].Value.ToString(), "D:\\test");
                      }
                  }

                  this.Invoke(new Action(() =>
                  {
                      this.button2.Enabled = true;

                  }));
              });
        }
    }
}
